import {
    getCurrentUserPagePermissionsApi,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsSuccess
} from 'Shared/actions/currentUserContext';
import {PAGE_CODE} from './constants';
import * as api from './api';
import {conventTableCondition} from '../utils';
/* =======================获取详情================================== */
export const GET_BRANDS_BEGIN = 'GET_BRANDS_BEGIN';
export const GET_BRANDS_SUCCESS = 'GET_BRANDS_SUCCESS';
export const GET_BRANDS_FAIL = 'GET_BRANDS_FAIL';
import {isYorN} from '../Enum';
export const getBrandBegin = () => ({
    type: GET_BRANDS_BEGIN
});
export const getBrandSuccess = data => ({
    type: GET_BRANDS_SUCCESS,
    data
});
export const getBrandFail = () => ({
    type: GET_BRANDS_FAIL,
});

export const getBrands = condition => dispatch => {
    dispatch(getBrandBegin());
    return api.getBrands(condition).then(res => {
        if(res.ok)
            dispatch(getBrandSuccess(res.data));
        else
            dispatch(getBrandFail());
        return res.ok;
    });
};

//*==============================初始化API================================= */
export const getInitData = (code = PAGE_CODE) => dispatch => {
    const init = dispatch(getBrands());
    const permissions = getCurrentUserPagePermissionsApi(code).then(res => {
        if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });
    return Promise.all([init, permissions]).then(values => values[0] && values[1]);
};
/*==============================查询列表================================= */

export const GET_LIST_BEGIN = 'GET_LIST_BEGIN';
export const GET_LIST_SUCCESS = 'GET_LIST_SUCCESS';
export const GET_LIST_FAIL = 'GET_LIST_FAIL';
export const getListBegin = () => ({
    type: GET_LIST_BEGIN
});
export const getListSuccess = (data, condition) => ({
    type: GET_LIST_SUCCESS,
    data,
    condition
});
export const getListFail = () => ({
    type: GET_LIST_FAIL,
}); //获取列表
const conventDate = (condition = {}) => {
    const {isNeedPicture, ...others} = condition;
    return {
        ...others,
        isNeedPicture: isNeedPicture ? isNeedPicture === isYorN.Y : undefined,
    };
};
export const getList = condition => dispatch => {
    dispatch(getListBegin());
    const queryCondition = conventTableCondition(conventDate(condition));
    return api.getList(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListSuccess(res.data, condition));
        else
            dispatch(getListFail());
        return res.ok;
    });
}; //按钮搜索

export const querySearch = () => (dispatch, getState) => {
    const condition = getState()
        .getIn(['page', 'appState', 'queryCondition'])
        .toJS();
    return dispatch(getList(condition));
}; //分页排序搜索

export const tableSearch = page => (dispatch, getState) => {
    const query = getState()
        .getIn(['page', 'appState', 'queryBySort'])
        .toJS();
    const condition = Object.assign({}, query, page);
    return dispatch(getList(condition));
};

/*==============================新增修改action================================= */
export const onNewOrUpdate = (data, details) => () => {
    const addCondition = {
        brandId: data.brandId,
        code: data.code,
        name: data.name,
        isNeedPicture: data.isNeedPicture ? data.isNeedPicture === 1 : undefined,
        remark: data.remark,
        details: details.map(v => ({
            productCategoryId: v.productCategoryId
        })),
    };
    const updateCondition = {
        brandId: data.brandId,
        name: data.name,
        isNeedPicture: data.isNeedPicture ? data.isNeedPicture === isYorN.Y : undefined,
        remark: data.remark,
        rowVersion: data.rowVersion,
        details: details.map(v => ({
            productCategoryId: v.productCategoryId
        })),
    };
    return data.id ? api.updateInfo(data.id, updateCondition)
        .then(res => ({success: res.ok})) : api.addInfo(addCondition).then(res => ({success: res.ok}));
};
/*=============================启用================================= */
export const onClickStart = id => (dispatch, getState) => {
    const data = getState()
        .getIn(['page', 'domainData', 'list', 'data'])
        .toJS();
    const {rowVersion} = data.find(item => item.id === id);
    return api.startInfo(id, {
        rowVersion
    }).then(res => res.ok);
};

/*=============================停用================================ */
export const onClickStop = id => (dispatch, getState) => {
    const data = getState()
        .getIn(['page', 'domainData', 'list', 'data'])
        .toJS();
    const {rowVersion} = data.find(item => item.id === id);
    return api.stopInfo(id, {
        rowVersion
    }).then(res => res.ok);
};

/*==============================查询action================================= */
//修改查询条件

export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件

export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});

